package com.example.ecas.persistence;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.ecas.pojo.Account;
import org.apache.ibatis.annotations.Mapper;


import java.util.List;


@Mapper
public interface AccountMapper extends BaseMapper<Account> {

    // 返回指定id的account
    default Account getAccount(String id) {
        return this.selectById(id);
    }

    // 返回指定电话的account
    default Account getAccountByPhone(String phone) {
        QueryWrapper<Account> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("phone", phone);
        List<Account> list = this.selectList(queryWrapper);
        if (list.isEmpty()) {
            return null;
        }
        return list.get(0);
    }

    // 返回指定邮箱的account
    default Account getAccountByEmail(String email) {
        QueryWrapper<Account> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("email", email);
        List<Account> list = this.selectList(queryWrapper);
        if (list.isEmpty()) {
            return null;
        }
        return list.get(0);
    }

    // 插入一个新account（注册），返回实际操作的行数
    default int insertNewAccount(Account account) {
        if (account == null) {
            return 0;
        }
        QueryWrapper<Account> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", account.getId());
        if (!this.exists(queryWrapper)) {
            return this.insert(account);
        }
        return 0;
    }
}
